package cn.wolfcode.service.impl;

import cn.wolfcode.domain.SystemMenu;
import cn.wolfcode.mapper.SystemMenuMapper;
import cn.wolfcode.qo.SystemMenuQueryObject;
import cn.wolfcode.service.ISystemMenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SystemMenuServiceImpl implements ISystemMenuService {
    @Autowired
    private SystemMenuMapper systemMenuMapper;

    @Override
    @Transactional
    public void delete(Long id) {
        systemMenuMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void insert(SystemMenu systemMenu) {
        if (systemMenu.getParentId() != null) {
            systemMenu.setParentName(systemMenuMapper.selectByPrimaryKey(systemMenu.getParentId()).getName());
        }
        systemMenuMapper.insert(systemMenu);

    }

    @Override
    public SystemMenu get(Long id) {
        return systemMenuMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SystemMenu> listAll() {
        return systemMenuMapper.selectAll();
    }

    @Override
    @Transactional
    public void update(SystemMenu systemMenu) {
        if (systemMenu.getParentId() != null) {
            systemMenu.setParentName(systemMenuMapper.selectByPrimaryKey(systemMenu.getParentId()).getName());
        }
        systemMenuMapper.updateByPrimaryKey(systemMenu);
    }

    @Override
    public PageInfo<SystemMenu> query(SystemMenuQueryObject qo) {
        // 使用分页插件，传入当前页，每页显示数量
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        // 返回查询一页展示的数据
        return new PageInfo<>(systemMenuMapper.selectForList(qo));
    }

    @Override
    public List<SystemMenu> queryBySn(String menuManager) {
        return systemMenuMapper.selectBySn(menuManager);
    }

    @Override
    public void updateStart(SystemMenu systemMenu,int status) {
        if (status == 1) {
            systemMenu.setStatus(0);
        } else {
            systemMenu.setStatus(1);
        }
        systemMenuMapper.updateStart(systemMenu);
    }
}
